package java8;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LambdaStream {
    public static void main(String[] args) throws IOException {
        List<String> nameStrs = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur");

        List<String> list = nameStrs.stream()//先转化为流
                .filter(s -> s.startsWith("L"))//过滤出 L 开头的所有元素
                .map(String::toUpperCase)//以L开头的所有元素的字母变成大写(这里的意思是用 String 的 toUpperCase 方法去处理)
                .sorted()//排序(函数的内部还可以写排序的规则)
                .collect(Collectors.toList());//将流再转变为一个 List
        System.out.println(list);

        //数组也可以被转化为流
        String[] array = {"a", "b", "c", "d", "e"};
        //Arrays.stream
        Stream<String> stream1 = Arrays.stream(array);
        stream1.forEach(x -> System.out.println(x));
        //Stream.of
        Stream<String> stream2 = Stream.of(array);
        stream2.forEach(x -> System.out.println(x));

        //文件中的元素也可以转化为流
        Stream<String> stringStream = Files.lines(Paths.get("file.txt"));
    }
}
